var _Scene = require('component/scene.js');
var _layout = require('tools/layout.js');
var _relative = _layout.relative;
var _TouchNode = require('event/touchnode.js').TouchNode;

var _global = require('global/global.js');
var R = require('res/texture.js').R;
var S = require('res/style.js');

var _History = require('config/history.js');
var _Menu = require('scenes/menu.js');
var _UIUtils = require('scenes/game/utils.js');
var _Constant = require('scenes/game/constant.js');

var _Prefer = require('config/prefer.js');
var _BackListener = require('global/backlistener.js');

// ==========================
// Cover
// ==========================
var Cover = _Scene.createScene(
    function () {
        var screen = _global.screen;
        var w = screen.width(), h = screen.height();
        this.setId('cover');

        this.addChild(this.mTitle = module.$('./cover/title.js'));
        this.addChild(this.mGrids = module.$('./cover/grid.js'));
        this.addChild(this.mPlay = _UIUtils.createLongButton(R.ui.bt_play_up, R.ui.bt_play_down, 'play', 'PLAY'));
//        this.addChild(this.mBonus = module.$('./cover/bonus.js'));// hangover description
//        this.getEventNode().setDelegate(this.mBonus);

        this.mGrids.on('touchslot', this.onGridClick, this);// touch high score
        this.mPlay.on('click', this.onButtonClick, this);// play button
        this.querySelector('#title button').forEach(function (button) {
            button.on('click', this.onButtonClick, this);
        }, this);

        this.onSizeChange(w, h);
    }
);
// ---------------- bind touch
Cover.prototype.onTouch = function (event) {
};
Cover.prototype.createEventNode = function () {
    return new _TouchNode(this);
};
// ----------- backpress
Cover.prototype.onEnter = function () {
    _BackListener.setActive(false);
};
Cover.prototype.onExit = function () {
    _BackListener.setActive(true);
};
// ----------- event process
Cover.prototype.onButtonClick = function (button) {
    _global.cancelEvents();
    switch (button.getId()) {
        case 'menu':
            this.startMenu();
            break;
        case 'toturial':
            this.startToturial();
            break;
        case 'play':
            if (_Prefer.isToturialfinished()) {
                this.startGame();
            } else {
                this.startToturial();
            }
            break;
    }
};

// ---------------- enter
Cover.prototype.showCover = function (game) {
    require('scenes/widget/sceneanima.js')
        .createAnima(game, this)
        .direction('down')
        .start();
};

// ---------------- exist
Cover.prototype.startMenu = function () {
    _BackListener.setActive(true);
    _Menu.newInstance(_History.getRecords()).showMenu(this);
};
Cover.prototype.startToturial = function () {
    var gameLocal = require('./game/constant.js').getLocal();
    gameLocal.add('istoturial', true);
    this.startGame();
};
Cover.prototype.startGame = function () {
    var game = require('scenes/game.js').newInstance();
    require('scenes/widget/sceneanima.js')
        .createAnima(this, game)
        .direction('up')
        .start();
};

Cover.prototype.onGridClick = function (number) {
    //console.log('Cover.prototype.onGridClick', number);
    //this.mBonus.show(number);
    _global.cancelEvents();
};
Cover.prototype.onSizeChange = function (w, h) {
    this.setSize(w, h);
    var playBottom = 130;// incase show ads

    _relative.local.layoutTo(this.mTitle, 0.5, 1, this, 0.5, 1);
    _relative.layout(this.mPlay, 0.5, 0, w * 0.5, playBottom);

    var margin = 20;
    var top = h - this.mTitle.height() - margin;
    var bottom = playBottom + this.mPlay.height();
    var remain = top - bottom;
    var center = bottom + remain * 0.58;// grid center
    var gridtop = center + this.mGrids.height() * 0.5;
    if (gridtop > top) {
        center -= gridtop - top;
    }

    _relative.layout(this.mGrids, 0.5, 0.5, w * 0.5, center);
//    this.mBonus.setShowPosition(w * 0.5, center + _Constant.slotSize[1] * 0.5);
};

module.exports = Cover;